from rest_framework import generics, permissions
from rest_framework.response import Response
from .models import BrowsingHistory
from .serializers import BrowsingHistorySerializer

class BrowsingHistoryListView(generics.ListAPIView):
    serializer_class = BrowsingHistorySerializer
    permission_classes = [permissions.IsAuthenticated]
    
    def get_queryset(self):
        return BrowsingHistory.objects.filter(user=self.request.user)

class ClearHistoryView(generics.GenericAPIView):
    permission_classes = [permissions.IsAuthenticated]
    
    def post(self, request):
        deleted, _ = BrowsingHistory.objects.filter(user=request.user).delete()
        return Response({
            'message': f'已清除 {deleted} 条浏览记录',
            'count': deleted
        })